package com.lnsystem.service.impl;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import org.apache.commons.mail.EmailAttachment;
import org.apache.commons.mail.MultiPartEmail;
import org.apache.log4j.Logger;
import org.joda.time.DateTime;
import org.joda.time.LocalDate;
import org.springframework.stereotype.Component;

import com.lnsystem.model.Stock;
import com.lnsystem.model.StockDailyData;
import com.lnsystem.service.ReportingService;

@Component
public class ReportingServiceHTMLImpl implements ReportingService {

	private static Logger log = Logger
			.getLogger(ReportingServiceHTMLImpl.class);
	
	private static final String REPORTS_PATH = "/home/guru/dev/LN/reports";

	@Override
	public String generateReportOfStockPicks(String reportName, String title, 
			List<Stock> stockPicks) {
		BufferedReader br = null;

		try {
			StringBuilder htmlResponse = new StringBuilder();
			StringBuilder stockPickHtml = new StringBuilder();
			
			br = new BufferedReader(new FileReader(this.getClass()
					.getClassLoader().getResource("empty-report.html")
					.getFile()));
			String line = br.readLine();

			if (line != null) {

				htmlResponse.append(line+"\n");

				for (Stock stock : stockPicks) {
					stockPickHtml.append("<tr>");
					stockPickHtml.append("<td>" + stock.getSymbol() + "</td>");


					StringBuilder notes = new StringBuilder();
					
/*					
					if(stock.getPocketPivots() != null && stock.getPocketPivots().size()>0){
						for (StockDailyData pivots : stock.getPocketPivots()) {
							if(pivots.getNotes()!=null){
								notes.append(";" + pivots.getNotes());
							}
						}
					}
					if(stock.getSmCross()!= null && stock.getSmCross().size()>0){
						for (StockDailyData smaCross : stock.getSmCross()) {
							if(smaCross.getNotes()!=null){
								notes.append(";" + smaCross.getNotes());
							}
						}
					}
					stockPickHtml.append("<td>" + notes + "</td>");

*/					
					stockPickHtml.append("<td>" + stock.getNotes() + "</td>");
					
					if(stock.getHealthString()!=null) {
						stockPickHtml.append("<td>"+stock.getHealthString()+"</td>");
					} else {
						stockPickHtml.append("<td>&nbsp;</td>");
					}
					
					
					stockPickHtml.append("<td><a href='#' id='finviz-"
							+ stock.getSymbol()
							+ "-daily-chartlink'>d</a></td>");
					stockPickHtml.append("<td><a href='#' id='finviz-"
							+ stock.getSymbol()
							+ "-weekly-chartlink'>w</a></td>");
					stockPickHtml.append("<td><a href='#' id='stockcharts-"
							+ stock.getSymbol()
							+ "-daily-chartlink'>d</a></td>");
					stockPickHtml.append("<td><a href='#' id='stockcharts-"
							+ stock.getSymbol()
							+ "-weekly-chartlink'>w</a></td>");
					stockPickHtml.append("<td><a href='#' id='ibd-"
							+ stock.getSymbol()
							+ "-daily-chartlink'>d</a></td>");
					stockPickHtml.append("<td><a href='#' id='ibd-"
							+ stock.getSymbol()
							+ "-weekly-chartlink'>w</a></td>");

					stockPickHtml.append("</tr>\n");
					
				}
				htmlResponse.append(stockPickHtml);
				htmlResponse.append("   </tbody> </table> ");
				htmlResponse.append("   <h4>"+title+"</h4>");
				htmlResponse.append("   </body> </html>");

				log.debug(">>> HTML Response:");
				log.debug(htmlResponse);

				DateTime now = DateTime.now();
			
				String reportFileName = REPORTS_PATH+"/lnsystem-" + reportName +LocalDate.now()+"T"+now.getHourOfDay()+"-"+now.getMinuteOfHour()+"-"+now.getSecondOfMinute()+".html";
				PrintWriter reportFile = new PrintWriter(new FileWriter(reportFileName));
				reportFile.println(htmlResponse);
				reportFile.close();

				//emailReport(reportFileName);
				
				
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				br.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		return null;
	}

	@Override
	public void emailReport(String path, String description) {
		  // Create the attachment
		  EmailAttachment attachment = new EmailAttachment();
		  attachment.setPath(path);
		  attachment.setDisposition(EmailAttachment.ATTACHMENT);
		  attachment.setDescription(description);
		  attachment.setName(description);

		  try {
			  // Create the email message
			  MultiPartEmail email = new MultiPartEmail();
			  email.setHostName("smtp.gmail.com");
			  email.setSmtpPort(587);
			  email.addTo("s.guruprasanna@gmail.com", "Guru");
			  email.setFrom("s.guruprasanna@gmail.com", "Guru");
			  email.setSubject(description);
			  email.setMsg(description);

			  // add the attachment
			  email.attach(attachment);

			  // send the email
			  email.send();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}

	}

}